
package com.android.fitnesscenter.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.PaintDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ActivityHelper;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.widget.CustomDatePickerDialog;

public abstract class SelectBaseActivity extends BaseActivity {

    protected String mCurUser, mCurOperate, mCurDate;
    protected int mCurType;
    protected PopupWindow mPopupWindow;
    protected int mPopupWindowWidth;

    protected TextView txt_time;

    protected TextView txt_user;

    protected TextView txt_operate;

    protected String[] mOperateStrs;

    protected ArrayList<MapEntity> mList;

    protected boolean isDialogOk = false;

    @Override
    protected void initTitleBar(String titleStr) {
        super.initTitleBar(titleStr);
        LinearLayout include_base_select_layout = (LinearLayout) findViewById(R.id.include_base_select_layout);
        txt_user = (TextView) include_base_select_layout.findViewById(R.id.txt_user);
        txt_user.setOnClickListener(mOnClickListener);
        txt_operate = (TextView) include_base_select_layout.findViewById(R.id.txt_operate);
        txt_operate.setOnClickListener(mOnClickListener);
        txt_time = (TextView) include_base_select_layout.findViewById(R.id.txt_time);
        txt_time.setOnClickListener(mOnClickListener);
    }

    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.txt_user:
                    titlebarMethod(R.id.txt_user);
                    break;
                case R.id.txt_operate:
                    titlebarMethod(R.id.txt_operate);
                    break;
                case R.id.txt_time:
                    titlebarMethod(R.id.txt_time);
                    break;
            }
        }
    };

    private CustomDatePickerDialog mCustomDatePickerDialog;

    protected void selectTime() {
        final Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        if (mCustomDatePickerDialog == null) {
            mCustomDatePickerDialog = new CustomDatePickerDialog(SelectBaseActivity.this, mOnDateSetListener, year,
                    month, cal.get(Calendar.DAY_OF_MONTH));
        } else {
            mCustomDatePickerDialog.updateDate(year, month + 1, 0);
        }
        isDialogOk = false;
        mCustomDatePickerDialog.setTitle(year + "年" + (month + 1) + "月");
        mCustomDatePickerDialog.show();
        DatePicker dp = findDatePicker((ViewGroup) mCustomDatePickerDialog.getWindow().getDecorView());
        if (dp != null) {
            int sdk = android.os.Build.VERSION.SDK_INT; // SDK号
            String language = Locale.getDefault().getLanguage();
            Loger.e("lanuage is " + language);
            if (!TextUtils.isEmpty(language)) {
                if (language.contains("en")) {
                    if (sdk > 10) {
                        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(
                                View.GONE);
                    } else {
                        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).setVisibility(View.GONE);
                    }
                } else {
                    if (sdk > 10) {
                        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(
                                View.GONE);
                    } else {
                        ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(2)).setVisibility(View.GONE);
                    }
                }
            } else {
                if (sdk > 10) {
                    ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
                } else {
                    ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(2)).setVisibility(View.GONE);
                }
            }
        }
        mCustomDatePickerDialog.setCanceledOnTouchOutside(false);
        mCustomDatePickerDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (!isDialogOk) {
                    mCurDate = "";
                    txt_time.setText(getString(R.string.txt_time));
                    Loger.e("onCancel is " + mCurDate);
                }
                requestData(getToken(), mCurUser, mCurOperate, mCurDate);
            }
        });
        mCustomDatePickerDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                isDialogOk = false;
            }
        });
    }

    private OnDateSetListener mOnDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            isDialogOk = true;
            mCurDate = year + "_" + (monthOfYear + 1);
            Loger.e("onDateSet is " + mCurDate);
            txt_time.setText(year + "年" + (monthOfYear + 1) + "月");
        }

    };

    protected void showPopWindows() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
        if (mPopupWindow == null) {
            createPopupWindow();
        }
        switch (mCurType) {
            case 0:
                mPopupWindow.showAsDropDown(txt_user);
                break;
            case 1:
                mPopupWindow.showAsDropDown(txt_operate);
                break;
        }
    }

    protected int mScreenWidth;

    private void createPopupWindow() {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View popupWindow = layoutInflater.inflate(R.layout.query_detail_popwindows_layout, null);
        ListView list_view = (ListView) popupWindow.findViewById(R.id.list_view);
        SimpleAdapter adapter = new SimpleAdapter(this, getDataByType(),
                R.layout.query_detail_popwindows_list_item, new String[] {
                        "title"
                },
                new int[] {
                        R.id.txt_title
                });
        list_view.setAdapter(adapter);
        list_view.setOnItemClickListener(popItemClickListener);
        if (mScreenWidth == 0) {
            mScreenWidth = ActivityHelper.getScreenWidth(this) / 3;
        }
        mPopupWindow = new PopupWindow(popupWindow, mScreenWidth, LayoutParams.WRAP_CONTENT);
        mPopupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        mPopupWindow.setTouchable(true); // 设置PopupWindow可触摸
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new PaintDrawable());
        mPopupWindowWidth = mPopupWindow.getWidth();
    }

    private OnItemClickListener popItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (mCurType) {
                case 0:
                    if (position == 0) {
                        mCurUser = "";
                        txt_user.setText(getString(R.string.txt_user));
                    } else {
                        mCurUser = mUserStrs[position];
                        txt_user.setText(mCurUser);
                    }
                    if (mPopupWindow != null) {
                        mPopupWindow.dismiss();
                    }
                    requestData(getToken(), mCurUser, mCurOperate, mCurDate);
                    break;
                case 1:
                    if (position == 0) {
                        mCurOperate = "";
                        txt_operate.setText(getString(R.string.txt_operate));
                    } else {
                        mCurOperate = mOperateStrs[position];
                        txt_operate.setText(mCurOperate);
                    }
                    if (mPopupWindow != null) {
                        mPopupWindow.dismiss();
                    }
                    requestData(getToken(), mCurUser, mCurOperate, mCurDate);
                    break;
                case 2:
                    break;
            }
        }
    };

    @Override
    protected void parseQueryMembersByTypeResult() {
        mCurType = 0;
        showPopWindows();
    }

    protected abstract List<HashMap<String, String>> getDataByType();

    protected abstract void requestData(String token, String user, String operate, String date);
}
